//
//  WPTreatmentInstructionsVC.m
//  wobingwoyi
//
//  Created by WGP on 2016/12/1.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPTreatmentInstructionsVC.h"
#import "WPTreatmentHeaderFooterView.h"
#import "WPMRModel.h"
#import "WPContentModel.h"
#import "WPDiagnosisModel.h"
#import "WPTreamentProgressListModel.h"
#import "WPTreamentSectionHeaderView.h"
#import "WPDiagnosisPictureCell.h"
#import "WPDiagnosisLabelCell.h"
#import "WPReadingVideoAndAudioCell.h"
#import "WPPlayController.h"
#import "WPVideoOrAudioModel.h"
#import "WPReadingVideoOrAudioPlayerVC.h"
#import "WPTreamentTableViewHeaderView.h"

@interface WPTreatmentInstructionsVC ()<UITableViewDelegate,UITableViewDataSource,MWPhotoBrowserDelegate>

@property(nonatomic,strong)NSMutableArray *models;
@property(nonatomic,strong)NSMutableArray *MWPhotos;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)BOOL isShowBigImage;
@property(nonatomic,strong)NSIndexPath *index;

@property(nonatomic,strong)NSMutableArray *allPicURLS;
@property(nonatomic,strong)NSArray *timesArr;

@end

@implementation WPTreatmentInstructionsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    [self setupTableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    if (self.isChargeExample)
    {
        //增加一个头部
        UIImage *image = [UIImage imageNamed:@"example_tip"];
        WPTreamentTableViewHeaderView *headerView = [[WPTreamentTableViewHeaderView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , image.size.height+20)];
        self.tableView.tableHeaderView = headerView;
    }
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)setupTableView
{
    [self tableView];
}

-(UITableView *)tableView
{
    if (_tableView == nil)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = GlobalBgColor;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.allowsSelection = NO;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        [_tableView registerClass:[WPTreatmentHeaderFooterView class]forHeaderFooterViewReuseIdentifier:@"WPTreatmentHeaderFooterView"];
        [_tableView registerClass:[WPTreamentSectionHeaderView class]forHeaderFooterViewReuseIdentifier:@"WPTreamentSectionHeaderView"];
        [self.view addSubview:_tableView];
        [_tableView registerClass:[WPDiagnosisLabelCell class] forCellReuseIdentifier:@"WPDiagnosisLabelCell"];
         [_tableView registerClass:[WPDiagnosisPictureCell class] forCellReuseIdentifier:@"WPDiagnosisPictureCell"];
        [_tableView registerClass:[WPReadingVideoAndAudioCell class] forCellReuseIdentifier:@"WPReadingVideoAndAudioCell"];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _tableView;
}

-(void)imagePickerView:(NSIndexPath *)indexPath
{
    // Modal
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
    nc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    browser.displayActionButton = NO;
    browser.displayNavArrows = YES;
    browser.alwaysShowControls = YES;
    [browser setCurrentPhotoIndex:indexPath.item];
    
    [self presentViewController:nc animated:YES completion:nil];
}

#pragma mark - MWPhotoBrower的代理方法
//返回图片个数
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
    return self.MWPhotos.count;
}

//返回图片模型
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
    
    if (index < self.MWPhotos.count)
    {
        return [self.MWPhotos objectAtIndex:index];
    }
    return nil;
    
}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    
    WPDiagnosisModel *firstModel = [WPDiagnosisModel initWithTime:model.treatmentTime arr:model.treatmentProcess_list];
    //音视频
    if(model.processMultfile_list.count)
    {
        [firstModel.textAndPicArr addObject:model.processMultfile_list];
    }
    [self.models addObject:firstModel];
    
    //首诊图片
    for (NSString *url in model.treamentProcessArr)
    {
        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:url]];
        [self.MWPhotos addObject:photo];
        
        [self.allPicURLS addObject:url];
    }
    
    if (model.processDescp_list.count)
    {
        for (WPTreamentProgressListModel *liseModel in model.processDescp_list)
        {
            WPDiagnosisModel *diaModel = [WPDiagnosisModel initWithTime:liseModel.writeTime arr:liseModel.descp_list];
            
            if(liseModel.multFile_list.count)
            {
                [diaModel.textAndPicArr addObject:liseModel.multFile_list];
            }
            
            [self.models addObject:diaModel];
            
            
            if(liseModel.descpURLS.count)
            {
                for (NSString *url in liseModel.descpURLS)
                {
                    MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:url]];
                    [self.MWPhotos addObject:photo];
                    
                    [self.allPicURLS addObject:url];
                }
            }
        }
    }
    
    
}

#pragma mark - tableView的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.models.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    WPDiagnosisModel *model = self.models[section];
    return model.textAndPicArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPDiagnosisModel *model = self.models[indexPath.section];
    
    if ([model.textAndPicArr[indexPath.item] isKindOfClass:[WPContentModel class]])
    {
        WPContentModel *contentModel = model.textAndPicArr[indexPath.item];
        
        if ([contentModel.content hasPrefix:@"http://"])
        {
            WPDiagnosisPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPDiagnosisPictureCell"];
            
            cell.refreshCell = ^(NSIndexPath *indexPath){
                
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            };
            
            cell.showAllPicture = ^(NSString *url){
                
                [self.allPicURLS enumerateObjectsUsingBlock:^(NSString  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    if ([obj isEqualToString:url])
                    {
                        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
                        [self imagePickerView:indexPath];
                        
                        *stop = YES;
                    }
                }];
                
            };
            
            cell.showAllBigPicture = ^{
                
                //            for (WPDiagnosisModel *diagnosisModel in self.models)
                //            {
                //                for (WPContentModel *contentModel in diagnosisModel.textAndPicArr)
                //                {
                //                    if (contentModel.scale)
                //                    {
                //                        contentModel.width = SCREEN_WIDTH-2*10;
                //                        contentModel.height = contentModel.width/contentModel.scale;
                //                    }
                //                    else
                //                    {
                //                        //图片还没有加载出来的时候
                //
                //                    }
                //                }
                //            }
                //
                //            [self.tableView reloadData];
            };
            
            cell.model = contentModel;
            cell.url = contentModel.content;
            cell.indexPath = indexPath;
            
            if (!self.isShowBigImage)
            {
                cell.isShowBigImage = YES;
                self.isShowBigImage = YES;
                self.index = indexPath;
            }
            else
            {
                cell.isShowBigImage = NO;
                if (self.index.section == indexPath.section && self.index.item == indexPath.item)
                {
                    cell.isShowBigImage = YES;
                }
            }
            
            return cell;
        }
        else
        {
            WPDiagnosisLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPDiagnosisLabelCell"];
            cell.model = contentModel;
            cell.str = contentModel.content;
            return cell;
        }
    }
    else
    {
        //音视频
        WPReadingVideoAndAudioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPReadingVideoAndAudioCell"];
        cell.models = model.textAndPicArr[indexPath.item];
        cell.audioOrVideoDidClickBlock = ^(NSInteger index){
        
            WPReadingVideoOrAudioPlayerVC *avpc = [WPReadingVideoOrAudioPlayerVC new];
            avpc.videoAndAudioArr = model.textAndPicArr[indexPath.item];
            avpc.selectedIndex = index;
            [self presentViewController:avpc animated:YES completion:nil];
            
        };
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WPDiagnosisModel *model = self.models[indexPath.section];
    WPContentModel *contentModel = model.textAndPicArr[indexPath.item];
    
    if ([model.textAndPicArr[indexPath.item] isKindOfClass:[WPContentModel class]])
    {
        if ([contentModel.content hasPrefix:@"http://"])
        {
            return contentModel.height;
        }
        else
        {
            CGSize size = [contentModel.content sizeWithMaxSize:CGSizeMake(SCREEN_WIDTH-2*10, CGFLOAT_MAX) font:14];
            return size.height+2 + 30;
        }
    }
    else if([model.textAndPicArr[indexPath.item] isKindOfClass:[NSArray class]])
    {
        NSArray *arr = model.textAndPicArr[indexPath.item];
        
        if (arr.count)
        {
            NSInteger row = (arr.count -1) / 3;
            
            CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
            CGFloat itemHeight = itemWidth*90/112.5;
            
            return (row + 1)*itemHeight + row*10 + 15;
        }
        else
        {
            return 10;
        }
    }
    else
    {
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 1)
    {
        return 88;
    }
    else
    {
        return 44;
    }
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WPDiagnosisModel *model = self.models[section];
    
    if (section == 0)
    {
        WPTreatmentHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WPTreatmentHeaderFooterView"];
        view.isFooter = NO;
        view.isChargeExample = self.isChargeExample;
        view.time = [NSString stringWithFormat:@"症状时间:  %@",model.time];
        return view;
    }
    else if (section == 1)
    {
        WPTreatmentHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WPTreatmentHeaderFooterView"];
        view.isFooter = YES;
        view.isChargeExample = self.isChargeExample;
        view.time = [NSString stringWithFormat:@"一诊治疗时间:  %@",model.time];
        return view;
    }
    else
    {
        WPTreamentSectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WPTreamentSectionHeaderView"];
         view.time = [NSString stringWithFormat:@"%@诊治疗时间:  %@",self.timesArr[section-1],model.time];
        return view;
    }
}

-(NSMutableArray *)MWPhotos
{
    if (_MWPhotos == nil)
    {
        _MWPhotos = [NSMutableArray array];
    }
    return _MWPhotos;
}

-(NSMutableArray *)models
{
    if (_models == nil)
    {
        _models = [NSMutableArray array];
    }
    return _models;
}

-(NSMutableArray *)allPicURLS
{
    if (_allPicURLS == nil)
    {
        _allPicURLS = [NSMutableArray array];
    }
    return _allPicURLS;
}

-(NSArray *)timesArr
{
    if (_timesArr == nil)
    {
        _timesArr = [NSArray arrayWithObjects:@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二", nil];
    }
    return _timesArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
